package com.hengxinguotong.zhihuichengjian.ui;

import android.app.Activity;
import android.content.Intent;
import android.widget.Toast;

import com.hengxinguotong.zhihuichengjian.bean.IntentBean;
import com.hengxinguotong.zhihuichengjian.bean.JumpBean;
import com.hengxinguotong.zhihuichengjian.bean.MenuBean;
import com.hengxinguotong.zhihuichengjian.bean.User;
import com.hengxinguotong.zhihuichengjian.utils.Constants;
import com.hengxinguotong.zhihuichengjian.utils.SPUtil;

import java.io.Serializable;

/**
 * Created by Administrator on 2017/3/4.
 */

public class ActivityJumper {

    /**
     * 修改添加人员信息
     */
    public static void toAddPersonnelActivity(int type, Activity activity, User user) {
        Intent modIntent = new Intent(activity, AddPersonnelActivity.class);
        modIntent.putExtra("type", type);
        if (type == AddPersonnelActivity.MODTYPE) {
            modIntent.putExtra("user", user);
        }
        activity.startActivityForResult(modIntent, 1);
    }

    /**
     * 添加/修改班组人员
     */
    public static void toAddGroupActivity(int type, Activity activity, String teamId, String teamName) {
        Intent addIntent = new Intent(activity, AddGroupActivity.class);
        addIntent.putExtra("type", type);
        if (type == AddGroupActivity.EDITTYPE) {
            addIntent.putExtra("teamId", teamId);
            addIntent.putExtra("teamName", teamName);
        }
        activity.startActivityForResult(addIntent, 1);
    }

    /**
     * 根据url跳转到相应的功能模块
     *
     * @param menuBean
     * @param activity
     */
    public static void jumpByURL(MenuBean menuBean, Activity activity) {
        Intent addIntent = new Intent();
        JumpBean jumpBean = Constants.indexHashMap.get(menuBean.getId());

        if (jumpBean == null) {
            return;
        }

        String uuid = SPUtil.getString(activity, "uuid");
        String constructionid = SPUtil.getString(activity, "constructionid");

        //材料预算  /material/toBudget/{uuid}?constructionId=xxxxxxx
        if (menuBean.getId() == 610) {
            jumpToWebView("/materialBudget/toBudget/" + uuid + "?constructionId=" + constructionid, menuBean, activity);
            return;
        }
//        if (menuBean.getId() == 610) {
//            jumpToWebView("/material/toBudget/" + uuid + "?constructionId=" + constructionid, menuBean.getName(), activity);
//            return;
//        }

        //材料统计  /material/toStatistics/{uuid}?constructionId=xxxxxxx
        if (menuBean.getId() == 640) {
            jumpToWebView("/material/toStatistics/" + uuid + "?constructionId=" + constructionid, menuBean, activity);
            return;
        }

        //工程信息  /construction/toProjectInfo/{uuid}?constructionId=xxxxxxx
        if (menuBean.getId() == 840) {
            jumpToWebView("/construction/toProjectInfo/" + uuid + "?constructionId=" + constructionid, menuBean, activity);
            return;
        }

        //总体进度计划 /construction/toScheduledPlan/{uuid}?constructionId=xxxxxxx
//        if (menuBean.getId() == 850) {
//            jumpToWebView("/scheduledPlan/toAllProgress/" + uuid + "?constructionId=" + constructionid, menuBean.getName(), activity);
//            activity.startActivity(intent);
//            return;
//        }
//        if (menuBean.getId() == 850) {
//            jumpToWebView("/construction/toScheduledPlan/" + uuid + "?constructionId=" + constructionid, menuBean.getName(), activity);
//            return;
//        }

        //工程进度 / construction/toProjectSchedule/{uuid}?constructionId=xxxxxxx
//        if (menuBean.getId() == 870) {
//            jumpToWebView("/projectSchedule/toProgress/" + uuid + "?constructionId=" + constructionid, menuBean.getName(), activity);
//            activity.startActivity(intent);
//            return;
//        }
//        if (menuBean.getId() == 870) {
//            jumpToWebView("/construction/toProjectSchedule/" + uuid + "?constructionId=" + constructionid, menuBean.getName(), activity);
//            return;
//        }

        //工程结构 /construction/toProjectStructure/{uuid}?constructionId=xxxxxxx
//        if (menuBean.getId() == 860) {
//            jumpToWebView("/construction/toProjectStructure/" + uuid + "?constructionId=" + constructionid, menuBean.getName(), activity);
//            activity.startActivity(intent);
//            return;
//        }

        //设备监测 /equipmentRegister/toReport/{uuid}?constructionId=xxxxxxx
        if (menuBean.getId() == 560) {
            jumpToWebView("/equipmentRegister/toReport/" + uuid + "?constructionId=" + constructionid, menuBean.getName(), activity);
            return;
        }

        //环境监测 /environment/toReport/{uuid}?constructionId=xxxxxxx
        if (menuBean.getId() == 700) {
            jumpToWebView("/environment/toReport/" + uuid + "?constructionId=" + constructionid, menuBean.getName(), activity);
            return;
        }

        //考勤监测 /environment/toReport/{uuid}?constructionId=xxxxxxx
        if (menuBean.getId() == 240) {
            jumpToWebView("/person/toAttendance/" + uuid + "?constructionId=" + constructionid, menuBean, activity);
            return;
        }

        //考勤登记 /environment/toReport/{uuid}?constructionId=xxxxxxx
        if (menuBean.getId() == 250) {
            jumpToWebView("/attendance/toAttendanceCount/" + uuid + "?constructionId=" + constructionid, menuBean, activity);
            return;
        }

        //考勤查询 /environment/toReport/{uuid}?constructionId=xxxxxxx
        if (menuBean.getId() == 270) {
            jumpToWebView("/person/toSearch/" + uuid + "?constructionId=" + constructionid, menuBean, activity);
            return;
        }

        //高支模设置
        if (menuBean.getId() == 1310) {
            jumpToWebView("/highformwork/toSet/" + uuid + "?constructionId=" + constructionid + "&add=" + menuBean.getAuth().getAdd(), menuBean.getName(), activity);
            return;
        }

        //高支模监测
        if (menuBean.getId() == 1320) {
            jumpToWebView("/highformwork/toIndex/" + uuid + "?constructionId=" + constructionid, menuBean.getName(), activity);
            return;
        }


        if (jumpBean.getJumpClass() == null) {
            Toast.makeText(activity, "功能开发中", Toast.LENGTH_SHORT).show();
            return;
        }
        addIntent.setClass(activity, jumpBean.getJumpClass());
        addIntent.putExtra("title", menuBean.getName());
        addIntent.putExtra("authority", menuBean.getAuth());

        //适配有多个参数，目前最多只有一个参数
        for (IntentBean intentBean : jumpBean.getIntentBeanList()
                ) {
            addIntent.putExtra(intentBean.getName(), intentBean.getValue());
        }

        if(menuBean.isHasNew()){
            activity.startActivityForResult(addIntent, 1);
        }else{
            activity.startActivity(addIntent);
        }
    }

    public static void jumpToMenu(MenuBean menuBean, Activity activity) {
        if (menuBean.getData() == null || menuBean.getData().size() == 0) {
            return;
        }Intent intent = new Intent(activity, MenuActivity.class);
        intent.putExtra("title", menuBean.getName());
        intent.putExtra("menu", menuBean);
        intent.putExtra("list", (Serializable) menuBean.getData());
        if(menuBean.isHasNew()){
            activity.startActivityForResult(intent, 1);
        }else{
            activity.startActivity(intent);
        }
    }

    public static void jumpToWebView(String url, String title, Activity activity) {
        Intent intent = new Intent(activity, WebViewActivity.class);
        intent.putExtra("url", url);
        intent.putExtra("title", title);
        activity.startActivity(intent);
    }

    public static void jumpToWebView(String url, MenuBean menu, Activity activity) {
        Intent intent = new Intent(activity, WebViewActivity.class);
        intent.putExtra("url", url);
        intent.putExtra("title", menu.getName());
        intent.putExtra("authority", menu.getAuth());
        activity.startActivity(intent);
    }


}
